package com.brother.template.quart;

import com.brother.template.inflxudb.model.Idwelltime;
import com.brother.template.inflxudb.model.Ipassengerflow;
import com.brother.template.inflxudb.service.InfluxdbDwelltimeService;
import com.brother.template.inflxudb.service.InfluxdbFlowService;
import com.brother.template.mapper.ZoneMapper;
import com.brother.template.model.Zone;
import com.brother.template.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.sql.Time;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Auther: xjw
 * @Date: 2018/10/22 11:10
 * @Description:
 */
@Component
public class TodayService implements SchedulingConfigurer {
    private Logger log = LoggerFactory.getLogger(TodayService.class);

    @Value("${todayRun}")
    private String todayRun;
    @Value("${influxdbUrl}")
    private String influxdbUrl;
    @Value("${influxdbName}")
    private String influxdbName;
    @Value("${influxdbTimeZone}")
    private String influxdbTimeZone;
    @Autowired
    private ZoneMapper zoneMapper;
    @Autowired
    private InfluxdbFlowService influxdbFlowService;

    @Autowired
    private InfluxdbDwelltimeService influxdbDwelltimeService;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                log.error("===== start TodayService ===");
                common(new Date());
            }

        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                //顶死任务触发，可修改定时任务的执行周期
                CronTrigger trigger = new CronTrigger(todayRun);
                Date nextExecDate = trigger.nextExecutionTime(triggerContext);
                return nextExecDate;
            }
        });
    }

    public void common(Date dateDate) {
        log.error("==============" + TimeUtil.getDateTime(dateDate));
        int dayOfMonth = TimeUtil.UDateToLocalDateTime(dateDate).getDayOfMonth();
        List<Zone> list = zoneMapper.selectList(null);
        List<String> dates = new ArrayList<>();
        for (int i = 0; i < 61; i++) {
            dates.add(TimeUtil.getSpecifiledDay(TimeUtil.getDateOfString("2018-02-01"), i, "yyyy-MM-dd"));
        }

        String date = TimeUtil.getDate(dateDate);
        String startTime = dates.get(dayOfMonth) + " 00:00:00";
        String endTime = dates.get(dayOfMonth) + " " + TimeUtil.getSdf("HH:mm:ss").format(dateDate);
        log.error("=== startTime " + startTime);
        log.error("=== endTime " + endTime);
        log.error("=== date " + date);

        list.forEach(zone -> {
            List<Integer> zoneIds = new ArrayList<>();
            zoneIds.add(zone.getId());

            List<Ipassengerflow> ipassengerflows = influxdbFlowService.selectflow(influxdbTimeZone, influxdbUrl, influxdbName, "passengerflow", zoneIds, startTime, endTime);
            List<Ipassengerflow> ipassengerflows_business = influxdbFlowService.selectflow(influxdbTimeZone, influxdbUrl, influxdbName, "passengerflow_business", zoneIds, startTime, endTime);
            if (ipassengerflows != null) {
                ipassengerflows.forEach(it -> {
                    it.setTime(date + " " + TimeUtil.getTimeOfString(it.getTime()));
                });
                log.error("===ipassengerflows " + ipassengerflows.size());
                influxdbFlowService.batchInsertPassengerflow(influxdbUrl, influxdbName, ipassengerflows);
            }
            if (ipassengerflows_business != null) {
                ipassengerflows_business.forEach(it -> {
                    it.setTime(date + " " + TimeUtil.getTimeOfString(it.getTime()));
                });
                log.error("===ipassengerflows_business " + ipassengerflows_business.size());
                influxdbFlowService.batchInsertPassengerflow_business(influxdbUrl, influxdbName, ipassengerflows_business);
            }

            List<Idwelltime> idwelltimes = influxdbDwelltimeService.selectDwelltime(influxdbTimeZone, influxdbUrl, influxdbName, "dwelltime", zoneIds, startTime, endTime);
            List<Idwelltime> idwelltimes_business = influxdbDwelltimeService.selectDwelltime(influxdbTimeZone, influxdbUrl, influxdbName, "dwelltime_business", zoneIds, startTime, endTime);
            if (idwelltimes != null) {
                idwelltimes.forEach(it -> {
                    it.setTime(date + " " + TimeUtil.getTimeOfString(it.getTime()));
                });
                log.error("===idwelltimes " + idwelltimes.size());
                influxdbDwelltimeService.batchInsertDwelltime(influxdbUrl, influxdbName, idwelltimes);
            }
            if (idwelltimes_business != null) {
                idwelltimes_business.forEach(it -> {
                    it.setTime(date + " " + TimeUtil.getTimeOfString(it.getTime()));
                });
                log.error("===idwelltimes_business " + idwelltimes_business.size());
                influxdbDwelltimeService.batchInsertDwelltime_business(influxdbUrl, influxdbName, idwelltimes_business);
            }
        });
    }

}
